#include("/admin/common/header.html")
#include("/admin/common/footer.html")
#include("/admin/common/left.html")
#include("/admin/common/layout.html")
 
 #@layout()
 #define content()

<span class="layui-breadcrumb">
  <a href="#(ctx_path)/">首页</a>
  <a href="#(ctx_path)/admin/websiteContent"><cite>发布内容</cite></a>
</span>

<hr>
<br/>

<div class="layui-fluid">
	<div class="layui-row">
		<div class="layui-col-sm1">
			&nbsp;
		</div>
		<div class="layui-col-sm9">
			<div class="layui-form">
				<!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
			
				<div class="layui-form-item">
					<div class="layui-inline" id="treeSelect" >
						<label class="layui-form-label">请选择栏目</label>
					</div>
				</div>
			
			</div>
			
			<hr>
			
			<form class="layui-form" id="detailForm">
			
				<input type="hidden" name="id" value="#(vo.id)">
			
				<div class="layui-form-item">
					<label class="layui-form-label">标题</label>
					<div class="layui-input-block">
						<input type="text" name="title" placeholder="请输入" value="#(vo.title)"
							autocomplete="off" class="layui-input" 
							lay-verify="required">
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">发布时间</label>
					<div class="layui-input-block">
						<input type="text" name="publishTime" id="publishTime" placeholder="请输入" value="#(DateKit.format(vo.publishTime))"
							autocomplete="off" class="layui-input" readonly
							lay-verify="required">
					</div>
				</div>
				
				<div class="layui-form-item">
					<label class="layui-form-label">内容</label>
					<div class="layui-input-block">
						<textarea name="content" id="content" name="content">#(attribute_map.content)</textarea>
					</div>
				</div>
				
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button type="button" class="layui-btn" lay-submit lay-filter="detailForm">保存</button>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
				
			</form>
			
		</div>
	</div>
</div>


#end

#define js()

<script>

var websiteColumnId="#(vo.websiteColumnId)";

function initTree(){
	$("#treeElem").html("");
	$.ajax({
		type:'get',
		url:'#(ctx_path)/admin/websiteColumn/tree',
		data:'',
		success:function(data){
			if(data.code=='0'){
				nodes=data.data;
				
				layui.use('treeSelect',function(){
					var treeSelect=layui.treeSelect;
					treeSelect.init("#treeSelect",{
						nodes:nodes,
						data:websiteColumnId
					});
				});
				
			}
		}
	});
}

initTree();

myCkeditor('content' );


layui.use(['form','laydate','treeSelect'], function() {
	var form = layui.form;
	var $ = layui.$;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var treeSelect=layui.treeSelect;


	laydate.render({
    	elem: '#publishTime' //指定元素
  	});

	form.verify(layuiFormVerify);
	
	form.on('submit(detailForm)', function(data) {
		//建议使用ajax在此处提交。
		
		var obj={};
		var attribute={};
		obj.id=$(":input[name='id']").val();
		obj.publishTime=$(":input[name='publishTime']").val();

		var data=layui.treeSelect.getData("#treeSelect");
		var wid="";
		for(var i=0;i<data.length;i++){
			if(data[i]!=""){
				//取最后一个不为空的select为parentId
				wid=data[i];
			}
		}
		obj.websiteColumnId=wid;
		obj.title=$(":input[name='title']").val();
		
		attribute.content=CKEDITOR.instances.content.getData();
		obj.attribute=JSON.stringify(attribute);
		
		layer.load(1,{shade:0.5})
		$.ajax({
			type : 'post',
			url : '#(ctx_path)/admin/websiteContent/publish/save',
			data : obj,
			success : function(data) {
				if (data.code == "0") {
					layer.msg('保存成功！', {
						time : 600
					}, function() {
						window.location.href = "/admin/websiteContent";
					});
				}
			}
		});
		//此处使用异步的好处，服务器端可以直接验证，如果直接提交，提交完成以后，如果验证不通过，那么不方便处理错误的信息
		return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
	});

});

</script>

#end


